package hongcaosp.app.android.contact;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import hongcaosp.app.android.comment.IViewType;
import hongcaosp.app.android.common.TestConstants;
import hongcaosp.app.android.login.UserToken;
import hongcaosp.app.android.modle.DataCallBack;
import hongcaosp.app.android.modle.bean.ContactListWrapper;
import hongcaosp.app.android.modle.bean.IUser;
import hongcaosp.app.android.modle.bean.User;
import hongcaosp.app.android.modle.impl.UserModleImpl;
import hongcaosp.app.android.modle.mi.UserModle;
import xlj.lib.android.base.component.BasePresenter;

public class ContactPresenter extends BasePresenter {
    private IContactView contactView;
    private UserModle userModle;

    private List<User> userFollows;
    private List<User> goodFriends;

    public ContactPresenter(IContactView contactView) {
        this.contactView = contactView;
        userModle = new UserModleImpl();
    }

    public void filter(String userName) {
        List<IViewType> userList = new ArrayList<>();
        userList.add(new IHeader() {
            @Override
            public String getHeadStr() {
                return "好友";
            }
        });
        if (goodFriends != null) {
            for (User user:goodFriends) {
                if (user.getNickname().contains(userName)) {
                    userList.add(user);
                }
            }
        }
        userList.add(new IHeader() {
            @Override
            public String getHeadStr() {
                return "全部关注";
            }
        });
        if (userFollows != null) {
            for (User user:userFollows) {
                if (user.getNickname().contains(userName)) {
                    userList.add(user);
                }
            }
        }
        contactView.freshContactList(userList);
     }

    public void getContactList() {
        userModle.linkman(UserToken.getDefault().getToken(),
                new DataCallBack<ContactListWrapper>() {
            @Override
            public void onGetData(ContactListWrapper data) {
                super.onGetData(data);
                List<IViewType> userList = new ArrayList<>();
                userList.add(new IHeader() {
                    @Override
                    public String getHeadStr() {
                        return "好友";
                    }
                });
                goodFriends = data.getGoodFriend();
                if (data.getGoodFriend() != null) {

                    userList.addAll(data.getGoodFriend());
                }
                userList.add(new IHeader() {
                    @Override
                    public String getHeadStr() {
                        return "全部关注";
                    }
                });
                userFollows = data.getUserFollow();
                if (data.getUserFollow() != null) {

                    userList.addAll(data.getUserFollow());
                }
                contactView.freshContactList(userList);
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                contactView.freshContactList(null);
            }
        });
    }
}
